//
//  ViewController.m
//  09-省市区（2）
//
//  Created by 韩旭 on 16/7/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "Model.h"
#import "HX_Model.h"
#import "HX_Zone.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic, strong) NSMutableArray *arrSection;
@property (nonatomic, strong) NSMutableArray *arrCell;
@property (nonatomic, strong) NSMutableArray *arrAreas;
@end

@implementation ViewController

- (NSMutableArray *)arrDataSource {
    if (!_arrDataSource) {
        self.arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _arrAreas = [NSMutableArray array];
    _arrSection = [NSMutableArray array];
    _arrCell = [NSMutableArray array];
    self.navigationItem.title = @"省市区";
    self.arrDataSource = [[Model arrGroup] mutableCopy];
    self.view.backgroundColor = [UIColor redColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStylePlain];
    [self creatArray];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}
- (void)creatArray {
    for (NSDictionary *dic in _arrDataSource) {
        Model *model = [Model cityModelDic:dic];
        NSArray *arrCitys = dic[@"Citys"];
        for (NSDictionary *dicCity in arrCitys) {
            [model.Citys addObject: dicCity[@" ACityNames"]];
            NSArray *arrArea = dicCity[@"Ares"];
            for (NSDictionary *dicArea in arrArea) {
                [model.Ares addObject:dicArea[@"AreaNames"]];
                NSLog(@"%@", model.Ares);
            }
        }[_arrSection addObject:model];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _arrSection.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.arrSection[section] Citys] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:ID];
    }
    Model *model = self.arrSection[indexPath.section];
    cell.textLabel.text = model.Citys[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [_arrSection[section] AProvince];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HX_Zone *zone = [[HX_Zone alloc] init];
    NSLog(@"%@", _arrSection);
//    Model *model = [self.arrSection[indexPath.section] Citys][indexPath.row];
    [self.navigationController pushViewController:zone animated:YES];
}
@end